package com.atguigu.controller;

import com.atguigu.domain.Permission;
import com.atguigu.service.PerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PerController {
    @Autowired
    private PerService perService;

    @RequestMapping("findAll.do")
    public ModelAndView findAll() {
        ModelAndView mv = new ModelAndView();
        List<Permission> list = perService.findAll();
        mv.addObject("permissionList", list);
        mv.setViewName("permission-list");
        return mv;
    }

    @RequestMapping("save.do")
    public String findAll(Permission permission) {
        perService.save(permission);
        return "redirect:findAll.do";
    }

    @RequestMapping("findById.do")
    public ModelAndView findById(@RequestParam(name = "id", required = true) String id) {
        ModelAndView mv = new ModelAndView();
        Permission permission = perService.findById(id);
        mv.addObject("permission", permission);
        mv.setViewName("permission-show");
        return mv;
    }

    @RequestMapping("deletePermission.do")
    public String deletePermission(String id) {
        perService.deletePermission(id);
        return "redirect:findAll.do";
    }
}

